package com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.Meditor;

import com.google.common.collect.Lists;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ActivitiConstants;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ApplicationContextUtils;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.activiti.BpmConfNodeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.activiti.BpmConfNode;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.DailyThingsVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.MaintenanceVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.PatrolInspectionVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProblemVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.DailyThingsService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.MaintenanceService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.PatrolInspectionService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.ProblemService;
import com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.personrule.MultiRule;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * 中间操作类
 */
public class MeditorFactory {

    private static MeditorFactory mditorFactory = new MeditorFactory();

    private MeditorFactory() {

    }


    public static MeditorFactory getInstance() {
        if (mditorFactory == null) {
            mditorFactory = new MeditorFactory();
        }
        return mditorFactory;
    }

    /**
     * 得到流程开始人员
     *
     * @param userId
     * @return
     */
    public static String createStartUserIdAndNameStr(String userId) {
        JsmEmployeeService jsmEmployeeService = (JsmEmployeeService) ApplicationContextUtils.getBean("jsmEmployeeService");
        EmployeeVo employee = null;
        try {
            employee = jsmEmployeeService.findEmployeeById(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (employee == null) {
            return "";
        }
        return employee.getName();
    }


    /**
     * 得到日常事务
     *
     * @param businessKey 业务ID
     * @return
     */
    public static DailyThingsVo createDailyThings(String businessKey) {
        DailyThingsService dailyThingsService = (DailyThingsService) ApplicationContextUtils.getBean(DailyThingsService.class);
        return dailyThingsService.queryBeanById(businessKey);
    }

    /**
     * 得到维护保养
     *
     * @param businessKey 业务ID
     * @return
     */
    public static MaintenanceVo createMaintenance(String businessKey) throws Exception {
        MaintenanceService maintenanceService = (MaintenanceService) ApplicationContextUtils.getBean(MaintenanceService.class);
        return maintenanceService.queryVoById(businessKey);
    }


    /**
     * 得到问题处理实体类
     *
     * @param businessKey 业务ID
     * @return
     */
    public static ProblemVo createProblem(String businessKey) throws Exception {
        ProblemService problemService = (ProblemService) ApplicationContextUtils.getBean(ProblemService.class);
        ProblemVo problemVo = new ProblemVo();
        problemVo.setProblemId(businessKey);
        return problemService.queryVoById(problemVo);
    }


    /**
     * 得到巡回检查
     *
     * @param businessKey 业务ID
     * @return
     */
    public static PatrolInspectionVo createPatrolInspection(String businessKey) {
        PatrolInspectionService patrolInspectionService = (PatrolInspectionService) ApplicationContextUtils.getBean(PatrolInspectionService.class);
        return patrolInspectionService.queryVoById(businessKey);
    }

    /**
     * 处理多实例
     *
     * @param execution
     */
    public static void handleMulti(ActivityExecution execution) {
        BpmConfNodeMapper bpmConfNodeMapper = (BpmConfNodeMapper) ApplicationContextUtils.getBean(BpmConfNodeMapper.class);
        List<String> manualPersonList = (List<String>) execution.getVariable(ActivitiConstants.MANUAL_PERSON);
        BpmConfNode bpmConfUser = null;
        //手动赋值处理人
        if (!CollectionUtils.isEmpty(manualPersonList)) {
            //多实例会签
            execution.setVariable(ActivitiConstants.MUTIL_USERS, manualPersonList);
            //移除变量
            execution.removeVariable(ActivitiConstants.MANUAL_PERSON);

        } else if ((bpmConfUser = bpmConfNodeMapper.getBpmConfNodeByNodeIdAndPid(execution.getCurrentActivityId(), execution.getProcessDefinitionId())) != null) { //手动办理
            //多实例会签
            handleMulti(execution, bpmConfUser, ActivitiConstants.USER_TASK);

        } else { //任务自动处理
            handleMulti(execution, bpmConfUser, ActivitiConstants.AUTO_TASK);
        }
    }

    /**
     * 处理多任务节点
     *
     * @param execution
     * @param bpmConfUser
     * @param type
     */
    private static void handleMulti(ActivityExecution execution, BpmConfNode bpmConfUser, String type) {
        if (ActivitiConstants.AUTO_TASK.equals(type)) {
            //自动完成
            execution.setVariable(ActivitiConstants.MUTIL_USERS, Lists.newArrayList(ActivitiConstants.AUTO_COMPLETE_ASSIGN));
        } else {
            MultiRule.getInstance().assigneRule(execution, null, bpmConfUser);
        }

    }




}
